package net.huashitong.node.service;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.sys.filter.NodeNewschedulesModel;
import com.sirdc.modules.sys.service.SysTableService;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Project.entity.NewProjects;
import net.huashitong.Project.service.NewProjectsService;
import net.huashitong.node.entity.NodeNewschedules;
import net.huashitong.node.filter.NodeNewschedulesFilter;
import net.huashitong.node.filter.NodeNewtempletFilter;
import net.huashitong.oversee.entity.SupervisionTask;
import net.huashitong.oversee.service.SupervisionTaskService;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;

import net.huashitong.node.dao.NodeNewtempletDao;
import net.huashitong.node.entity.NodeNewtemplet;

@Service
public class NodeNewtempletService extends StringPKService<NodeNewtemplet> {
    @Autowired
    private NodeNewtempletDao dao;
    @Autowired
    private SysTableService tableService;
    @Lazy
    @Autowired
    private SupervisionTaskService taskService;
    @Lazy
    @Autowired
    private NodeNewschedulesService nodeNewschedulesService;


    @Override
    protected BaseDao<NodeNewtemplet, String> getDao() {
        return dao;
    }

    @Override
    protected void beforeSave(NodeNewtemplet obj) {
        obj.setSysId(tableService.updateMaxSysId("t_bnp_node_newtemplet", null));
    }

    public List<NodeNewtemplet> queryNodeNewtempletByNodeType(String nodeType) {
        if (nodeType == null || "".equals(nodeType)) {
            nodeType = "0";
        }
        return dao.queryNodeNewtempletByNodeType(nodeType);
    }

    //查询
    public List<NodeNewtemplet> getNodeTempletInfo(NodeNewtempletFilter filter) {
        if (StringUtils.isBlank(filter.getNodeType())) {
            filter.setNodeType("3");
        }
        return dao.getNodeTempletInfo(filter);
    }

    public List<NodeNewtemplet> queryNodeTempletInfo(NodeNewtempletFilter filter) {
        return dao.getNodeTempletInfo1(filter);
    }

    //部门角色节点查询
    public List<NodeNewtemplet> getNodeForAcc(NodeNewtempletFilter filter) {
        return dao.getNodeForAcc(filter);
    }

    public List<NodeNewtemplet> queryByNodeFilter(String nodeFilterId) {
        return dao.queryByNodeFilter(nodeFilterId);
    }

    public NodeNewtemplet queryNodeNewTempletByNode(NodeNewtempletFilter filter) {
        return dao.queryNodeNewTempletByNode(filter);
    }

    public List<String> getAllApprovalNodeId() {
        List<String> result = new ArrayList<>();
        List<NodeNewtemplet> nodeNewtemplets = dao.query();
        for (NodeNewtemplet temp : nodeNewtemplets) {
            if (StringUtils.isNotBlank(temp.getResUser()) && StringUtils.isNotBlank(temp.getOperatorUser())) {
                result.add(temp.getNodeId());
            }
        }
        return result;
    }

    //根据经办人名字手机号查询节点清单
    public List<NodeNewtemplet> queryByOperator(String phone, String name) {
        return dao.queryOperator(phone, name);
    }

    //根据责任人名字手机号查询节点清单
    public List<NodeNewtemplet> queryByResUser(String phone, String name) {
        return dao.queryResUser(phone, name);
    }


    /**
     * 推算节点流程层级-zzc
     *
     * @param projectId
     * @return
     */
    public List<List<String>> calculateLevel(String projectId) {
        List<List<String>> result = new ArrayList<>();
        NodeNewschedulesFilter filter = new NodeNewschedulesFilter();
        filter.setState("1");           //节点确认
        filter.setType("1");            //大节点
        filter.setProjectId(projectId);
        List<NodeNewschedules> bigNodes = nodeNewschedulesService.queryNoPageByFilter(filter);
        List<String> nodeIdList = new ArrayList<>();
        for (NodeNewschedules bigNode : bigNodes) {
            nodeIdList.add(bigNode.getNodeId());
        }
        int size = bigNodes.size();
        List<String> tempList = new ArrayList<>();
        while (size > 0) {
            List<String> list = new ArrayList<>();
            List<NodeNewschedules> delList = new ArrayList<>();
            if (tempList.size() == 0) {
                //如果临时集合为空，就去找第一层级的节点
                for (NodeNewschedules bigNode : bigNodes) {
                    if ("#".equals(bigNode.getPreNodeId())
                            || !nodeIdList.contains(bigNode.getPreNodeId())) {
                        if (!bigNode.getPreNodeId().contains(",")) {
                            list.add(bigNode.getNodeId());
                            delList.add(bigNode);
                        } else {
                            String[] arr = bigNode.getPreNodeId().split(",");
                            boolean flag = true;
                            for (String s : arr) {
                                if (nodeIdList.contains(s)) {
                                    flag = false;
                                    continue;
                                }
                            }
                            if (flag) {
                                list.add(bigNode.getNodeId());
                                delList.add(bigNode);
                            }
                        }

                    }
                }
            } else {
                for (NodeNewschedules bigNode : bigNodes) {
                    if (bigNode.getPreNodeId().contains(",")) {
                        String[] arr = bigNode.getPreNodeId().split(",");
                        boolean flag = true;
                        for (String s : arr) {
                            if (!tempList.contains(s) && nodeIdList.contains(s)) {
                                flag = false;
                                continue;
                            }
                        }
                        if (flag) {
                            list.add(bigNode.getNodeId());
                            delList.add(bigNode);
                        }
                    } else {
                        if (tempList.contains(bigNode.getPreNodeId())) {
                            list.add(bigNode.getNodeId());
                            delList.add(bigNode);
                        }
                    }
                }

            }
            if(list.size()==0){
                list.add(bigNodes.get(0).getNodeId());
                delList.add(bigNodes.get(0));
            }
            tempList.addAll(list);
            bigNodes.removeAll(delList);
            result.add(list);
            size = size - delList.size();
        }

        return result;
    }

    public List<NodeNewschedulesModel> addLevel(List<NodeNewschedulesModel> resule, List<List<String>> lists){

        for (NodeNewschedulesModel model : resule) {
            List<List<String>> m = new ArrayList<>();
            m.addAll(lists);
            //大节点
            if("1".equals(model.getType())){
                for (List<String> list : lists) {
                    if(!list.contains(model.getNodeId())){
                        m.remove(list);
                    }else{
                        break;
                    }
                }
                model.setProgressList(m);
            }
        }
        return resule;
    }

    public List<NodeNewschedulesModel> judgeAddTask(List<NodeNewschedulesModel> resultNodeModel){
        Map<String,String> nodeMap = new HashMap<>();
        List<SupervisionTask> tasks = taskService.query();
        for (SupervisionTask task : tasks) {
            if("1".equals(task.getAaa001())){
                if(!nodeMap.containsKey(task.getNodeSysId())){
                    nodeMap.put(task.getNodeSysId(),task.getSysId());
                }
            }
        }
        Map<String,String> smallNodeMap = new HashMap<>();
        for (NodeNewschedulesModel model : resultNodeModel) {
            if(nodeMap.containsKey(model.getSysId())){
                String taskId = nodeMap.get(model.getSysId());
                //小节点进入督查
                smallNodeMap.put(model.getNodeIds(),taskId);
                model.setTaskId(taskId);
            }else{
                model.setTaskId("0");
            }
        }
        for (NodeNewschedulesModel model : resultNodeModel) {
            if(smallNodeMap.containsKey(model.getNodeId())){
                String taskId = smallNodeMap.get(model.getNodeId());
                model.setTaskId(taskId);
            }
        }
        return resultNodeModel;
    }
}
